package com.wisdom.mybatisdemo.mapper;

import com.wisdom.mybatisdemo.model.UserInfo;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 11951
 * Date: 2025-03-02
 * Time: 19:20
 */
@Mapper
public interface UserInfoMapper {
//    1.查找所有用户信息
    @Select("select * from user_info")
    List<UserInfo> selectAll();
//    2.需求: 查找id=4的⽤⼾
    @Select("SELECT username,`password`,gender,phone from user_info WHERE id=4")
    UserInfo getId(Integer id);

//    3.使⽤ @Param
    @Select("SELECT username,`password`,gender,phone from user_info WHERE id=#{userid}")
    UserInfo getId2(@Param("userid") Integer id);
//    4.增加数据
//    @Insert("insert into user_info (username, `password`, age, gender, phone)  values (#{username},#{password},#{age},#{gender},#{phone})")
//    Integer insert(UserInfo userInfo);

    @Insert("insert into user_info (username, `password`, age, gender, phone)  values (#{userInfo.username},#{userInfo.password},#{userInfo.age},#{userInfo.gender},#{userInfo.phone})")
    Integer insert(@Param("userInfo") UserInfo userInfo);

//    5.删(Delete)
    @Delete("delete from user_info where id=#{id}")
    void delete(Integer id);

//    6.改(Update)

    @Update("update user_info set username=#{username}  where  id=#{id}")
    void  update(UserInfo userInfo);

//    7.查select
    @Select("select id, username, `password`, age, gender, phone, delete_flag, create_time, update_time from user_info")
    List<UserInfo> select1();
//    1.起别名
    @Select("select id, username, `password`, age, gender, phone, delete_flag  as deleteFlag" +
            ", create_time  as createTime, update_time  as updateTime from user_info")
    List<UserInfo> select2();
   // 2.结果映射
   @Results({
           @Result(column = "delete_flag", property = "deleteFlag"),
           @Result(column = "create_time", property = "createTime"),
           @Result(column = "update_time", property = "updateTime")
   })
    @Select("select * from user_info")
    List<UserInfo> selectAllUser();

//    3.开启驼峰命名
    @Select("select id, username, `password`, age, gender, phone, delete_flag, " +
            "create_time, update_time from user_info")
    public List<UserInfo> AllUser();
}
